﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using YuuzleComic.BLL;
using Newtonsoft.Json.Linq;
using System.IO;
using YuuzleComic.DBUtility;

public partial class admin_server : System.Web.UI.Page
{
    zj zjbll = new zj();
    Maticsoft.Common.WebClient wc = new Maticsoft.Common.WebClient();
    protected string JsonStr = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            switch (Request.QueryString["a"])
            {
                case "downloadComplete":
                    this.downloadComplete();
                    break;
                case "getTopDownload":
                    this.getTopDownload();
                    break;
                case "readPictures":
                    this.readPictures();
                    break;
                case "checkExistDirectory":
                    this.checkExistDirectory();
                    break;
                default:
                    JsonStr = JObject.Parse("{success:false,message:'参数错误'}").ToString();
                    break;
            }
        }
    }

    private void checkExistDirectory()
    {
        string sql = "select b.* from comics a join zj b on a.id=b.cid where (b.isdownload=0 or b.isdownload is null)";
        if (!string.IsNullOrEmpty(Request.QueryString["strwhere"]))
        {
            sql += " and " + Request.QueryString["strwhere"];
        }
        DataSet ds = DbHelperSQL.Query(sql);
        int count = 0;
        if (ds.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (System.IO.Directory.Exists(Server.MapPath("~/comicdata/" + dr["tpadd"])))//如果不存在就创建文件夹
                {
                    DbHelperSQL.ExecuteSql("update zj set isdownload=1 where id=" + dr["id"]);
                    count++;
                }
            }
        }
        Response.Write(count);
    }

    private void readPictures()
    {
        try
        {
            DirectoryInfo TheFolder = new DirectoryInfo(Server.MapPath("~/comicdata/" + HttpUtility.UrlDecode(Request.QueryString["tpadd"])));
            //遍历文件
            JsonStr = JArray.FromObject(TheFolder.GetFiles()).ToString();
        }
        catch (Exception e)
        {
            JsonStr = JObject.Parse("{error:'" + e.Message + "'}").ToString();
        }
        
    }

    private void getTopDownload()
    {
        try
        {
            DataSet ds = zjbll.GetTopDownload(1, Request.QueryString["strwhere"]);
            JsonStr = JArray.FromObject(ds.Tables[0]).ToString();
        }
        catch (Exception e)
        {
            JsonStr = JObject.Parse("{error:'" + e.Message + "'}").ToString();
        }
    }

    private void downloadComplete()
    {
        try
        {
            if (Request["zjid"] != null)
            {
                zjbll.downloadComplete(Convert.ToInt32(Request.QueryString["zjid"]));
            }
        }
        catch (Exception)
        {

            throw;
        }
    }
}